package com.dcloud.manage.dblib.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dcloud.manage.dblib.entity.DatalibCategory;
import com.dcloud.manage.dblib.entity.request.DbCateNodeReq;
import com.dcloud.manage.dblib.entity.vo.DatalibCategoryNodeVO;
import com.dcloud.manage.dblib.mapper.DatalibCategoryMapper;
import com.dcloud.manage.dblib.service.IDatalibCategoryService;
import com.dcloud.utils.SpringBeanUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

/**
 * @Description: datalib_category
 * @Author: jeecg-boot
 * @Date:   2024-03-11
 * @Version: V1.0
 */
@Service
public class DatalibCategoryServiceImpl extends ServiceImpl<DatalibCategoryMapper, DatalibCategory> implements IDatalibCategoryService {

    @Autowired
    private DatalibCategoryMapper datalibCategoryMapper;

    @Override
    public List<DatalibCategoryNodeVO> tree(DbCateNodeReq dbCateNodeReq) {

        //如果传了分类模糊拆查询字段，直接返回查询列表
        if(StringUtils.isNotEmpty(dbCateNodeReq.getCategoryName())){
            List<DatalibCategory> datalibCategories = datalibCategoryMapper.selectList(new LambdaQueryWrapper<DatalibCategory>()
                    .like(DatalibCategory::getCategoryName, dbCateNodeReq.getCategoryName()));
            return SpringBeanUtil.copyProperties(datalibCategories, DatalibCategoryNodeVO.class);
        }

        //根据名称模糊查询所有分类
        List<DatalibCategory> datalibCategories = datalibCategoryMapper.selectList(new LambdaQueryWrapper<DatalibCategory>());
        if(datalibCategories.isEmpty()){
            return null;
        }

        List<DatalibCategory> datalibCategoriesF = datalibCategoryMapper.selectList(new LambdaQueryWrapper<DatalibCategory>()
                .eq(DatalibCategory::getCategoryParCode, -1));

        // 遍历顶级类别并递归查询子类别
        List<DatalibCategoryNodeVO> result = new LinkedList<>();
        for (DatalibCategory topCategory : datalibCategoriesF) {
            List<DatalibCategoryNodeVO> children = findChildren(topCategory.getCategoryCode());
            DatalibCategoryNodeVO datalibCategoryNodeVO = SpringBeanUtil.copyProperties(topCategory, DatalibCategoryNodeVO.class);
            datalibCategoryNodeVO.setChildren(children);

            result.add(datalibCategoryNodeVO);
        }

        return result;
    }

    @Override
    public void saveCategory(DatalibCategory datalibCategory) {
        datalibCategory.setCategoryCode(UUID.randomUUID().toString().replace("-",""));
        datalibCategory.setCreateTime(new Date());
        if(StringUtils.equals(datalibCategory.getCategoryParCode(),"-1")){ //增加的父级
            datalibCategory.setCategoryLevel(1);
        }else {
            //获取父级的等级
            DatalibCategory datalibCategoryPar = datalibCategoryMapper.selectOne(new LambdaQueryWrapper<DatalibCategory>().eq(DatalibCategory::getCategoryCode, datalibCategory.getCategoryParCode()));
            datalibCategory.setCategoryLevel(datalibCategoryPar.getCategoryLevel()+1); //等级加1
        }

        datalibCategoryMapper.insert(datalibCategory);
    }

    @Override
    public void remove(String id) {
        DatalibCategory datalibCategory = datalibCategoryMapper.selectById(id);
        //判断是否有子级
        if(datalibCategoryMapper.selectCount(new LambdaQueryWrapper<DatalibCategory>().eq(DatalibCategory::getCategoryParCode, datalibCategory.getCategoryCode())) > 0){
            throw new RuntimeException("该分类下还有子级，不能删除");
        }

        //todo  后续需要判断分类下是否存在元素
        datalibCategoryMapper.deleteById(id);
    }

    private List<DatalibCategoryNodeVO> findChildren(String categoryCode) {
        List<DatalibCategoryNodeVO> children = new LinkedList<>();
        List<DatalibCategory> datalibCategories = datalibCategoryMapper.selectList(new LambdaQueryWrapper<DatalibCategory>()
                .eq(DatalibCategory::getCategoryParCode, categoryCode));
        if(datalibCategories == null || datalibCategories.isEmpty()){
            return children;
        }
        for(DatalibCategory child : datalibCategories){
            List<DatalibCategoryNodeVO> childrenChild = findChildren(child.getCategoryCode());
            DatalibCategoryNodeVO datalibCategoryNodeVO = SpringBeanUtil.copyProperties(child, DatalibCategoryNodeVO.class);
            datalibCategoryNodeVO.setChildren(childrenChild);

            children.add(datalibCategoryNodeVO);
        }

        return children;
    }
}
